<template>
  <div class="inputinvoice-detail">
    <van-field label="公司全称" label-width="90px" v-model="invoiceCompany" placeholder="请输入公司全称">
      <template #button>
        <van-button size="small" color="#f09637" @click="getTaxNo">匹配税号</van-button>
      </template>
    </van-field>
    <van-field label="企业税号" label-width="90px" v-model="taxpayerIdNum" placeholder="请输入纳税人识别号"></van-field>
    <van-field label="开户银行" v-model.trim="openBank" placeholder="请输入开户银行"></van-field>
    <van-field label="银行账号" v-model.trim="bankAccount" placeholder="请输入银行账号"></van-field>
    <van-field label="财务联系人" v-model.trim="financeContact" placeholder="请输入财务联系人姓名、电话"></van-field>
    <van-field label="财务邮箱" v-model.trim="email" placeholder="请输入财务邮箱"></van-field>
    <van-field name="radio" label="是否演示">
      <template #input>
        <van-radio-group v-model="needDemo" direction="horizontal">
          <van-radio name="需要" checked-color="#FE9200">需要</van-radio>
          <van-radio name="不需要" checked-color="#FE9200">不需要</van-radio>
        </van-radio-group>
      </template>
    </van-field>
    <van-field name="radio" label="演示时间" v-if="needDemo === '需要'">
      <template #input>
        <van-radio-group v-model="demoTime" direction="horizontal">
          <van-radio name="上午10:00" checked-color="#FE9200">上午10:00</van-radio>
          <van-radio name="下午16:00" checked-color="#FE9200">下午16:00</van-radio>
        </van-radio-group>
      </template>
    </van-field>
    <van-button color="#FE9200" block class="submit-btn" @click="submit">提交</van-button>
  </div>
</template>

<script>
import { Button, RadioGroup, Radio, Field, Toast, Dialog } from 'vant';
import { order_saveOrder, invoiceTitle_getCompanyList } from '@api/service';
export default {
  components: {
    [Field.name]: Field,
    [Button.name]: Button,
    [RadioGroup.name]: RadioGroup,
    [Radio.name]: Radio
  },
  data() {
    return {
      invoiceCompany: '', //公司名称
      taxpayerIdNum: '', //纳税人识别号
      openBank: '', //开户行
      bankAccount: '', //开户账号
      financeContact: '', //财务联系人
      email: '', //邮箱
      needDemo: '不需要', //是否需要演示
      demoTime: '上午10:00' //演示时间
    };
  },
  mounted() {
    this.invoiceCompany = this.userInfo.company;
  },
  methods: {
    //匹配税号
    async getTaxNo() {
      if (!this.invoiceCompany) {
        Toast('请输入公司全称');
      } else {
        let res = await invoiceTitle_getCompanyList({ keyword: this.invoiceCompany, pageSize: 1, pageNum: 1 });
        if (res.code === 200) {
          this.invoiceCompany = res.data.Result[0].Name;
          this.taxpayerIdNum = res.data.Result[0].CreditCode;
        } else {
          Toast(res.msg);
        }
      }
    },
    async submit() {
      const { invoiceCompany, taxpayerIdNum, openBank, bankAccount, financeContact, email, needDemo, demoTime } = this;
      if (!invoiceCompany) {
        return Toast('请填写公司全称');
      } else if (!taxpayerIdNum) {
        return Toast('请填写纳税人识别号');
      } else if (!openBank) {
        return Toast('请填写开户银行');
      } else if (!bankAccount) {
        return Toast('请填写银行卡号');
      } else if (!financeContact) {
        return Toast('请填写财务联系人');
      } else if (!email) {
        return Toast('请填写邮箱信息');
      }
      let detailList = [
        { itemKey: 'invoiceCompany', itemValue: invoiceCompany, itemName: '公司全称' },
        { itemKey: 'taxpayerIdNum', itemValue: taxpayerIdNum, itemName: '纳税人识别号' },
        { itemKey: 'openBank', itemValue: openBank, itemName: '开户银行' },
        { itemKey: 'bankAccount', itemValue: bankAccount, itemName: '银行卡号' },
        { itemKey: 'financeContact', itemValue: financeContact, itemName: '财务联系人' },
        { itemKey: 'email', itemValue: email, itemName: '邮箱' },
        { itemKey: 'needDemo', itemValue: needDemo, itemName: '是否演示' }
      ];
      if (needDemo === '需要') {
        detailList.push({ itemKey: 'demoTime', itemValue: demoTime, itemName: '演示时间' });
      }
      let { phone, nickname, company } = this.userInfo;
      let res = await order_saveOrder({
        json: 1,
        productId: this.$route.query.id,
        phone,
        nickname,
        company,
        detailList
      });
      if (res.code === 200) {
        Toast('提交成功，稍后我们的专属顾问会及时与您联系');
        setTimeout(() => {
          this.$router.go(-1);
        }, 1000);
      } else {
        Toast(res.msg);
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.inputinvoice-detail {
  overflow: hidden;
}
.inputinvoice-detail {
  padding: 0 32px;
  background: #fff;
}
.van-cell {
  align-items: center;
  padding: 20px 0;
  &:not(:last-child)::after {
    left: 0;
  }
}
.submit-btn {
  margin: 70px 0 50px;
  font-weight: bold;
  font-size: 30px;
  border-radius: 6px;
}
</style>
